package com.xnx3.wangmarket.plugin.serviceLog.controller.superadmin;

import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyun.openservices.log.exception.LogException;
import com.xnx3.BaseVO;
import com.xnx3.j2ee.pluginManage.controller.BasePluginController;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.vo.ActionLogListVO;
import com.xnx3.kefu.core.util.KefuPropertiesUtil;
import com.xnx3.kefu.core.util.MessageStorageUtil;
import com.xnx3.kefu.core.util.TokenUtil;
import com.xnx3.kefu.core.vo.MessageListVO;
import com.xnx3.wangmarket.plugin.serviceLog.MessageInterfaceImpl;

/**
 * 总管理后台中，看客服服务记录
 * @author 管雷鸣
 */
@Controller(value="ServiceLogPluginController")
@RequestMapping("/plugin/serviceLog/superadmin/")
public class ServiceLogPluginController extends BasePluginController{
	
	/**
	 * 列表页
	 */
	@RequestMapping("list.do")
	public String list(HttpServletRequest request, Model model){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		
		/*
		 * ActionLogUtil.list 参数说明：
		 * indexName 索引名字
		 * query 查询条件，当前示例是查询useraction中所有数据。可以传入条件进行筛选，传入如：  type:UPDATE_DATABASE AND userid:249
		 * everyPageNumber 每页显示几条，最大100
		 * request HttpServletRequest 分页会使用到这个
		 */
		ActionLogListVO vo = ActionLogUtil.list(MessageInterfaceImpl.INDEX, "", 50, request);
		if(vo.getResult() - ActionLogListVO.FAILURE == 0){
			//获取失败，返回失败提示
			return error(model, vo.getInfo());
		}
		
		model.addAttribute("list", vo.getJsonArray());
		model.addAttribute("page", vo.getPage());
		return "/plugin/serviceLog/superadmin/list";
	}
	
	/**
	 * 查看两个人的聊天日志记录
	 * @param chatId 聊天一方的chatid
	 * @param otherChatId 聊天另一方的chatid
	 */
	@RequestMapping("chatLog.do")
	public String chatLog(HttpServletRequest request, Model model,
			@RequestParam(value = "chatId", required = false, defaultValue = "") String chatId,
			@RequestParam(value = "otherChatId", required = false, defaultValue = "") String otherChatId){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		
		model.addAttribute("chatId", chatId);
		model.addAttribute("otherChatId", otherChatId);
		model.addAttribute("api", KefuPropertiesUtil.getProperty("kefu.api"));
		return "/plugin/serviceLog/superadmin/chatLog";
	}
	
	
	/**
	 * 查看两人的历史聊天记录
	 * @param chatId 聊天一方的chatid
	 * @param otherChatId 聊天另一方的chatid
	 * @param time 13位时间戳，要获取这个时间之前的记录
	 * @param number 要获取多少条记录，取值10~200之间，不传默认是100条
	 * @param type 类型，取数据的类型，可传入 before、after，  before:向前，也就是取<time的记录，after:向后,也就是取>time的记录 。默认不传则是before
	 * @return {@link com.xnx3.j2ee.vo.BaseVO} info: 信息列表
	 * @throws LogException 
	 */
	@ResponseBody
	@RequestMapping(value = "/chatLog${api.suffix}",method = {RequestMethod.POST})
	public MessageListVO chatLog(HttpServletRequest request,
			@RequestParam(value = "chatId", required = false, defaultValue = "") String chatId,
			@RequestParam(value = "otherChatId", required = false, defaultValue = "") String otherChatId,
			@RequestParam(value = "time", required = false, defaultValue = "999999999") long time
		){
		if(!haveSuperAdminAuth()){
			MessageListVO vo = new MessageListVO();
			vo.setBaseVO(BaseVO.FAILURE, "无权使用");
			return vo;
		}
		
		MessageListVO vo = MessageStorageUtil.chatLog(chatId, otherChatId, time, 200, "before", request); 
		
		ActionLogUtil.insert(request, "查看聊天历史日志列表", request.getQueryString());
		return vo;
	}
}
